// zh_jdgjb/pages/order/book.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrival_time: '01-01',
    departure_time: '01-01',
    days: 1,
    url: wx.getStorageSync("url")
  },

  detail: function() {
    wx.navigateTo({
      url: './orderinfo?id=' + this.data.id
    })
  },

  home: function() {
    wx.navigateTo({
      url: './../index/index'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    console.log(options);
    that.setData({
      id: options.orderid
    })
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    }), app.getUserInfo(function(e) {
      // console.log(e);
    });
    console.log(options.orderid);
    app.util.request({
      url: 'entry/wxapp/orderdetails',
      data: {
        order_id: that.data.id
      },
      success: function(e) {
        console.log(e);
        var t = e.data;
        that.setData({
          info: e.data,
          arrival_time: e.data.arrival_time.slice(5, 10),
          departure_time: e.data.departure_time.slice(5, 10),
          days: e.data.days,
          oldTime: t.time
        });
        if (t.status == 2) {
          that.timeout();
        }
        // if (t.status == 1) {
        //   that.timeout();
        // }
      }
    })
  },
  timeout: function() {
    var time = new Date((this.data.oldTime + '' + '000') * 1);
    console.log(time);
    var that = this;
    time = time.getTime() + 1000 * 60 * 10;
    // 时间初始化
    var nowTime = (new Date()).getTime();
    var a = parseInt((time - nowTime) / 1000);
    var m = parseInt(a / 60)
    var s = parseInt(a % 60)
    that.setData({
      timesa: '00:' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s) + ''
    })
    if (s == 0 && m == 0 || s < 0 || m < 0) {
      var info = that.data.info;
      info.status = '3';
      that.setData({
        timesa: '00:00:00',
        info: info
      })
      console.log(that.data.info);
    }

    var interVal = setInterval(function() {
      var nowTime = (new Date()).getTime();
      var a = parseInt((time - nowTime) / 1000);
      var m = parseInt(a / 60)
      var s = parseInt(a % 60)
      that.setData({
        timesa: '00:' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s) + ''
      })
      if (s == 0 && m == 0) {
        clearInterval(interVal);
        var info = that.data.info;
        info.status = '3';
        that.setData({
          timesa: '00:00:00',
          info: info
        })
      }
    }, 1000)

    var reqInter = setInterval(function() {
      app.util.request({
        url: 'entry/wxapp/orderdetails',
        data: {
          order_id: that.data.id
        },
        success: function(e) {
          console.log(e);
          var t = e.data;
          if (t.status != '2') {
            clearInterval(interVal);
            that.setData({
              info: t
            })
          }
        }
      })
    }, 5000)
  },
  orderStatus: function() {
    var o = this;
    setInterval(function() {
      app.util.request({
        url: 'entry/wxapp/orderdetails',
        data: {
          order_id: o.data.id
        },
        success: function(e) {
          var t = e.data;
          console.log(e);
          that.setData({
            status: t.status
          })
        }
      })
    }, 5000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  DateDiff: function(sDate1, sDate2) { //sDate1和sDate2是2006-12-18格式  
    var aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2006格式  
    aDate = sDate2.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数  
    return iDays
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})